<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <div>
        <el-row :gutter="20">
          <el-col :span="8">
            <!-- 搜索框 -->
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getAuthorityList">
              <el-button slot="append" icon="el-icon-search" @click="getAuthorityList"></el-button>
            </el-input>
          </el-col>
        </el-row>
        <el-table
          :data="authorityList"
          border
          stripe
        >
          <el-table-column type="index"></el-table-column>
          <el-table-column label="权限名称" prop="name">
          </el-table-column>
          <el-table-column prop="permission" label="权限标识">
          </el-table-column>
          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-tooltip effect="dark" content="修改权限名称" placement="top" :enterable="false">
                <el-button type="primary" icon="el-icon-edit" size="mini"
                           @click="updateDialogVisible = true;getUpdateForm(scope.row)"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[5, 10, 15]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
        <el-dialog
          title="修改权限名称"
          :visible.sync="updateDialogVisible"
          width="50%">
          <el-form ref="updateFormRef" :model="updateForm" :rules="updateFormRules" label-width="80px">
            <el-form-item prop="name" label="权限名称">
              <el-input v-model="updateForm.name" clearable></el-input>
            </el-form-item>
            <el-form-item prop="permission" label="权限标识">
              <el-input v-model="updateForm.permission" :disabled="true"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="updateDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateAuthority">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 5
      },
      total: 0,
      authorityList: [],
      updateDialogVisible: false,
      updateForm: {
        id: 0,
        name: '',
        permission: ''
      },
      updateFormRules: {
        name: [
          {
            required: true,
            message: '请输入权限名称',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.getAuthorityList()
  },
  methods: {
    async getAuthorityList () {
      const { data: res } = await this.$http.get('/authority', { params: this.queryInfo })
      if (res.code === 200) {
        this.authorityList = res.data.authorities.records
        this.total = res.data.authorities.total
      } else {
        this.$message.error('获取权限列表失效')
      }
    },
    async updateAuthority () {
      this.$refs.updateFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('/authority', this.updateForm)
        if (res.code === 200) {
          this.$message.success('更新权限信息成功')
          this.updateDialogVisible = false
          await this.getAuthorityList()
        } else {
          this.$message.error('更新权限信息失败')
        }
      })
    },
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      this.getAuthorityList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pageNum = newPage
      this.getAuthorityList()
    },
    getUpdateForm (info) {
      this.updateForm.id = info.id
      this.updateForm.name = info.name
      this.updateForm.permission = info.permission
    }
  }
}
</script>

<style lang="less" scoped>

</style>
